#include <iostream>
#include <cstdio>
#include <algorithm> 

using namespace std; 

#define N 1000001

struct node_s {
	node_s *left, *right; 
	int id; 
	int val; 
	int sum; 
	int nr_node; 
}; 

#define _pl() printf("line = %d, id=%d\n", __LINE__, root->id)
#define _pl1() printf("line = %d, id=%d, lc=%p/%d/%d, rc=%p/%d/%d\n", __LINE__, root->id, lc, lc?lc->id:-1, lc?lc->sum:-1, rc, rc?rc->id:-1, rc?rc->sum:-1)
#define _pl2() printf("line=%d, top=%d, %p:%d\n", __LINE__,  top, (top>0?stack[top-1]:0), (top>0?stack[top-1]->id:0))

struct stat_s {
	node_s	*node; 
	int		stat; 
}; 

struct iter_s {
	stat_s	stack[30]; 
	int		top; 
	void init(node_s *root) {
		top = 0; 
		if (root) {
			stack[top].node = root; 
			stack[top].stat = 0; 
			top++; 
		}
	}
	node_s *curr(void) {
		if (top==0) {
			return NULL; 
		}
		return stack[top-1].node; 
	}
	int left_next(void) {
		while (top>0) {
			int stat = stack[top-1].stat; 
			node_s *c = stack[top-1].node; 
			stack[top-1].stat++; 
			switch(stat) {
			case 0: 
				if (c->left) { 
					stack[top].node = c->left; 
					top++; 
					return 0; 
				}
				break; 
			case 1: 
				if (c->right) {
					stack[top].node = c->right; 
					top++; 
					return 0; 
				} 
				break; 
			default: 
				top--; 
				break; 
			}
		}
		return -1; 
	}
	int right_next(void) {
		while (top>0) {
			int stat = stack[top-1].stat; 
			node_s *c = stack[top-1].node; 
			stack[top-1].stat++; 
			switch(stat) {
			case 1: 
				if (c->left) { 
					stack[top].node = c->left; 
					top++; 
					return 0; 
				}
				break; 
			case 0: 
				if (c->right) {
					stack[top].node = c->right; 
					top++; 
					return 0; 
				} 
				break; 
			default: 
				top--; 
				break; 
			}
		}
		return -1; 
	}
}; 

node_s nd[N];
int n; 

void traval(node_s *root)
{
	//printf("traval: id=%d\n", root->id); 
	root->sum = root->val; 
	root->nr_node = 1; 
	if (root->left) {
		traval(root->left); 
		root->sum += root->left->sum; 
		root->nr_node += root->left->nr_node; 
	}
	if (root->right) {
		traval(root->right); 
		root->sum += root->right->sum; 
		root->nr_node += root->right->nr_node; 
	}
	//printf("id=%d, sum=%d, nr_node=%d\n", root->id, root->sum, root->nr_node); 
}


int is_symmetric(node_s *root)
{
	iter_s li, ri; 
	node_s *lc, *rc; 
	li.init(root->left); 
	ri.init(root->right); 
	lc = li.curr(); 
	rc = ri.curr(); 
	while (1) {
		lc = li.curr(); 
		rc = ri.curr(); 
		if (!lc && !rc) {
			return 1; 
		} if (lc && rc) {
			if (lc->sum!=rc->sum) {
				return 0; 
			}
			if (lc->val!=rc->val) {
				return 0; 
			}
		} else {
			return 0; 
		}
		li.left_next(); 
		ri.right_next(); 
	}
}

int traval_1(node_s *root)
{
	int rr, rl, ret; 
	rr = rl = 0; 

	if (root->left==NULL && root->right==NULL) {
		return 1; 
	} 
	if (is_symmetric(root)) {
		return root->nr_node; 
	} 
	if (root->left) {
		rl = traval_1(root->left); 
	} 
	if (root->right) {
		rr = traval_1(root->right); 
	}
	ret = max(rl, rr); 
	//printf("%d: ret=%d\n", root->id, ret); 
	return ret; 
}

int main()
{
	int i, l, r; 
	
	//freopen("p5018_3.in", "r", stdin); 
	
	cin>>n; 
	for (i=1; i<=n; i++) {
		cin>>nd[i].val; 
		nd[i].id = i; 
		nd[i].sum = nd[i].nr_node = 0; 
		nd[i].left = nd[i].right = NULL; 
	}
	
	for (i=1; i<=n; i++) {
		cin>>l>>r; 
		if (l!=-1)
			nd[i].left = nd+l; 
		if (r!=-1)
			nd[i].right = nd+r; 
	}

	traval(nd+1); 
/*	
	for (i=1; i<=n; i++) {
		node_s *lp, *rp, *p;
		p = nd+i;  
		lp = nd[i].left ? nd[i].left : nd; 
		rp = nd[i].right ? nd[i].right : nd; 
		printf("%d: %d %d sum=%d, val=%d\n", i, lp->id, rp->id, p->sum, p->val); 
	}
*/
	i = traval_1(nd+1); 
	printf("%d\n", i); 
	return 0; 
}

